﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.WebApp.Models.Ado;
using XSTDZ.ERP.Infrastructure.Authentication;
using XSTDZ.ERP.Infrastructure.Extentions;
using XSTDZ.ERP.WebApp.Models;

namespace XSTDZ.ERP.WebApp.Controllers
{
    //产品控制器
    public class ProductController : Controller
    {
        private readonly IProductService productServiceImpl = ServiceLocator.Instance.GetService<IProductService>();
        //
        // GET: /Product/
        public ActionResult Index()
        {
            return View();
        }
        #region 查看详细
        [NoAuthorizated("Detail")]
        public ActionResult Detail(Guid id)
        {
            var billModel = productServiceImpl.GetProductInfoDto(id);
            GetProductUint(billModel.ProductUint.ToString());
            return View(billModel);
        }
        #endregion
        #region 添加
        [HttpGet]
        [NoAuthorizated("Add")]
        public ActionResult Add()
        {
            GetProductUint("");
            return View();
        }
        [HttpPost]
        [NoAuthorizated("Add")]
        public ActionResult Add(ProductInfoDto productInfoDto)
        {
            if (ModelState.IsValid)
            {
                if (new BLLQuery().ValidateProductIsExist(productInfoDto))
                {
                    ModelState.AddModelError("", "已存在，无法添加");
                    ViewBag.PostState = 8;

                }
                else
                {
                    productInfoDto.ProductPyName = PYHelper.GetFirstPYLetter(productInfoDto.ProductName);
                    productServiceImpl.AddProduct(productInfoDto);
                    ViewBag.PostState = 9;
                }
            }
            GetProductUint(productInfoDto.ProductUint.ToString());
            return View();
        }
        #endregion
        #region 修改
        [HttpGet]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(Guid id)
        {
            var product = productServiceImpl.GetProductInfoDto(id);
            GetProductUint(product.ProductUint.ToString());
            return View(product);
        }
        [HttpPost]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(ProductInfoDto productInfoDto)
        {
            if (ModelState.IsValid)
            {
                //if (new BLLQuery().ValidateProductIsExist(productInfoDto))
                //{
                //    ModelState.AddModelError("", "已存在，无法添加");
                //    ViewBag.PostState = 8;

                //}
                //else
                //{
                    productInfoDto.ProductPyName = PYHelper.GetFirstPYLetter(productInfoDto.ProductName);
                    productServiceImpl.UpdateProduct(productInfoDto);
                    ViewBag.PostState = 9;
               // }
            }
            GetProductUint(productInfoDto.ProductUint.ToString());
            return View();
        }
        #endregion
        #region 删除
        [HttpGet]
        [NoAuthorizated("Delete")]
        public ActionResult Delete(Guid id)
        {
            productServiceImpl.ConvertProductDeleteState(id);
            return Content("ok");
        }
        #endregion
        #region 获得产品数据列表
         [NoAuthorizated("SelectProducts")]
        public ActionResult SelectProducts()
        {
            return View();
        }
         [NoAuthorizated("GetJsonOfProducts")]
        public ActionResult GetJsonOfProducts()
        {
            #region EF的查询，效率低下，故暂时放弃不用
            /*
            var products = productServiceImpl.GetProductInfoDto(1, 100,false);
            if (products != null && products.Count() != 0)
            {
                var jsonResult = products.Select(u => { u.DeleteState = u.Deleted == 0 ? "正常" : "已删除"; return u; });
                var target = Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult);
                target = "{\"Rows\":" + target + ",\"Total\":\"10\"}";
                return Content(target);
            }
            return View();
            */
            #endregion
            return Content(new BLLQuery().GetProductLists(this.HttpContext));
        }
        #endregion
        //获得计量单位
        private void GetProductUint(string ProductUintCode)
        {
            var model = new RefeModel();
            var refeList = model.GetRefeByModuleName(RefeTypeOption.计量单位);
            ViewBag.ProductUintList = model.GetDropDownListItemsByCode(refeList, ProductUintCode);
        }
       
    }
}
